/**
 * Created by rongyao on 2017/9/25.
 */
/**
 * Created by rongyao on 2017/9/22.
 */
requirejs.config({
	baseUrl: "../js/lib",
	paths: {
		"jquery": "jquery-3.2.1",
		"cookie": "jquery.cookie"
	}
})

requirejs(['jquery','cookie'], function ($) {
	
	//鼠标悬停，显示购物车列表
	$(".nav_right").on("mouseenter", function () {
		$(".hid_goods").css("height", $(".goods_list").height());
	});
	//鼠标离开，消失
	$(".nav_right").on("mouseleave", function () {
		$(".hid_goods").css("height", 0);
	});
	
	$.ajax({
		url: "../json/second.json",
		type: "get",
		async: true,
		success: function (res) {
			$(".nav_left>li:not([class*='hh'])").each(function (index, ele) {
				//动态创建li加a
				for (var i = 0; i < res[index].hotActivities.length; i++) {
					var $li = $("<li></li>");
					$li.html("<a href=''>" + res[index].hotActivities[i] + "</a>");
					$(ele).find("ul").eq(0).append($li);
				}
				for (var j = 0; j < res[index].goods.length; j++) {
					var $li = $("<li></li>");
					$li.html("<a href=''>" + res[index].goods[j] + "</a>");
					$(ele).find("ul").eq(1).append($li);
				}
				//鼠标移入 显示二级列表
				$(ele).on("mouseenter", function () {
					$(ele).find(".pop_up").css("display", "block");
				})
				//鼠标移出二级列表消失
				$(ele).on("mouseleave", function () {
					$(ele).find(".pop_up").css("display", "none");
				})
			})
			
		}
	})
	
	
	//动态添加内容
	//获取通过url地址传过来的值
	var xx = window.location.href.split("?");
	var arr = xx[1].split("&");
	var obj1 = {};
	for (var k = 0; k < arr.length; k++) {
		var arr1 = arr[k].split("=")
		obj1[arr1[0]] = arr1[1]
	}
	var username = obj1.username;
	
	console.log(obj1);
	
	//点击logo 跳转主页面
	$("#logo a").on("click",function () {
		window.location.href = "index.html?username="+username+"";
	})
	//点击加减改变数量购物袋
	$(".topLeft").html("<li></li><li class='user'><a href=''>" + username + "</a></li><li class='tui'><a>退出</a></li><li>|</li><li><a href=''>收藏</a></li>");
	//点击退出
	$(".tui").on("click", function () {
		
		$.removeCookie("users");
		
		window.location.href = "index.html";
	})
	
	//购物车init
	$.ajax({
		url: "http://localhost/hz1706/mei/init.php",
		dataType: "json",
		type: "post",
		data: {
			username: username
		},
		success: function (res) {
			console.log(res);
			$(".goods_list").html('');
			if (res.data[0].shopGoods) {
				var arr = JSON.parse(res.data[0].shopGoods);
				for (var i = 0; i < arr.length; i++) {
					$(".goods_list").append("<dl class='clear'><dt><img src=" + arr[i].src + " alt=''></dt><dd><p><a href=''>" + arr[i].goodsname + "</a><span>" + arr[i].count + "*" + arr[i].price + "</span></p><a class='remove'>删除</a></dd></dl>");
					//切割逗号
					var aa=arr[i].price;
					if (arr[i].price.indexOf(",") != -1) {
						var arr2 = arr[i].price.split(",");
						var aa = arr2[0] + arr2[1];
					}
					
					//一条一条显示购物车数据
					$(".goodMsg_wrap").append("<ul class='goodMsg'><li class='color'><input type='checkbox'><img src="+arr[i].src+" alt=''></li><li><h5 class='product_title'>"+arr[i].goodsname+"</h5></li><li id='productRMB'>￥"+arr[i].price+".00</li><li class='count clear'><a class='dis'>-</a><span>"+arr[i].count+"</span><a class='add'>+</a></li><li>￥119.40</li><li>￥"+arr[i].count*aa+"</li><li><a class='remove'>删除</a></li></ul>");
				}
				
			}
			
			
			//右边购物车里边显示商品数量和总价格
			priceSum();
		}
	})
	
	$(".goodMsg_wrap").on("click",".add,.dis", function () {
		var self = this;
		var flag;
		if(this.className=="add"){
			flag=1;
			var num = parseInt($(this).prev("span").text());
			$(this).prev("span").text(num + 1);
		}
		if(this.className=="dis"){
			flag=-1;
			var num = parseInt($(this).siblings("span").text());
			if(num<=1){
				flag=0;
			}
			if (num - 1 <= 1) {
				num = 2;
			}
			
			$(this).siblings("span").text(num - 1);
		}
		$.ajax({
			url: "http://localhost/hz1706/mei/shopCarin.php",
			dataType: "json",
			type: "post",
			data: {
				username: username,
				dates: {
					goodsname: $(self).parents(".goodMsg").find(".product_title").text(),
					count: flag,
					price: $(self).parents(".goodMsg").find("#productRMB").text(),
					src: $(self).parents(".goodMsg").find("img").attr("src")
				}
			},
			success: function (res) {
				console.log(res);
				//重新加载 将内容先清空
				$(".goods_list").html('');
				if (res.data[0].shopGoods) {
					var arr = JSON.parse(res.data[0].shopGoods);
					for (var i = 0; i < arr.length; i++) {
						$(".goods_list").append("<dl class='clear'><dt><img src=" + arr[i].src + " alt=''></dt><dd><p><a href=''>" + arr[i].goodsname + "</a><span>" + arr[i].count + "*" + arr[i].price + "</span></p><a class='remove'>删除</a></dd></dl>");
					}
				}
				
				//右边购物车里边显示商品数量和总价格
				priceSum();
				
			}
		})
		// $(".color img").attr("src")
		// $(".product_title").text()
		// $(".product_title").text()
		// $(".count span").text()
		// $("#productRMB").text()
		
	})
	
	
	function priceSum() {
		$(".shopping_quantity span").eq(0).text($("dl").length);
		var sum = 0;
		$("dl").each(function (index, ele) {
			var str = $(ele).find("span").text();
			var arr = str.split("*");
			var price=arr[1];
			if (arr[1].indexOf(",") != -1) {
				var arr1 = arr[1].split(",");
				 price = arr1[0] + arr1[1];
			}
			sum += Number(arr[0]) * Number(price);
		})
		$(".shopping_amount").text("¥" + sum + ".00");
	}
	
	//点击删除 删除数据 以及购物车里的相应内容
	$(".goods_list").on("click", ".remove", function () {
		var self = this;
		$.ajax({
			url: "http://localhost/hz1706/mei/shopCarout.php",
			dataType: "json",
			data: {
				username: username,
				goodsname: $(self).siblings("p").children("a").text()
			},
			type: "post",
			success: function (res) {
				$(self).parents("dl").remove();
				//右边购物车里边显示商品数量和总价格
				priceSum();
			}
		})
		
	})
	
	//点击列表里边的删除 删除数据 以及购物车里的相应内容
	$(".goodMsg_wrap").on("click", ".remove", function () {
		var self = this;
		$.ajax({
			url: "http://localhost/hz1706/mei/shopCarout.php",
			dataType: "json",
			data: {
				username: username,
				goodsname: $(self).parents(".goodMsg").find("h5").text()
			},
			type: "post",
			success: function (res) {
				var index = $(self).parents(".goodMsg").index();
				$(self).parents(".goodMsg").remove();
				$("dl").eq(index).remove();
				priceSum();
			}
		})
		
	})
	
	//将导航条的高度记下 方便后面判断nav是否要固定顶部
	var topY = $("#nav").offset().top;
	//记下main的marginTop值 滚轮往下滚 margin-top值减小 知道0
	var marginT = $("#main").css("marginTop");
	
	var nowMargin;
	$(window).on("scroll", function () {
		//导航条固定顶部判断
		if ($(document).scrollTop() >= topY) {
			//scrollTop值大于nav的offsetTop 将nav固定在顶部
			$("#logo").css("marginBottom", "42px");//原先位置用margin代替
			$("#nav").css({
				"position": "fixed",
				"top": 0,
				"width": "100%"
			});
		} else {
			//否则回到原处
			$("#logo").css("marginBottom", "0px");
			$("#nav").css({
				"position": "static"
			});
		}
	})
	
	//全选
	$('.allchoose').on("click",function () {
		$("input[type='checkbox']").prop("checked",$(this).prop('checked'));
	})
	
	setInterval(function () {
		var n=0;
		var zong=0;
		$(".goodMsg_wrap input").each(function (index,ele) {
			
				var text = $(ele).parents(".goodMsg").find("li").eq(2).text();
				var very = text.split("￥")[1];
				if(very.indexOf(",")!=-1){
					var every = very.split(",");
					very = every[0]+every[1];
				}
				var count = $(ele).parents(".goodMsg").find(".count span").text();
				$(ele).parents(".goodMsg").find("li").eq(5).text("￥"+Number(very)*Number(count));
			if($(ele).prop("checked")==true){
				n++;
				zong+=Number(very)*Number(count);
			}
			
		});
		
		//实时获取显示小计价格
		
		

		// 显示选中总价格
		$(".total_left i").text(n);
		$(".total_right i").text(zong);
		if(n>0){
			$(".total_right a").css("background","black");
		}else{
			$(".total_right a").css("background","");
		}
		
		//如果购物车没有商品 底部栏消失
		if($(".goodMsg").length==0){
			$("#total").css("display","none");
		}else{
			$("#total").css("display","block");
		}
	},100)
	
	
	//点击底部删除 删除选中的商品
	$(".total_left a").on("click",function () {
		$(".goodMsg_wrap input").each(function (index,ele) {
			if($(ele).prop("checked")==true){
				$.ajax({
					url: "http://localhost/hz1706/mei/shopCarout.php",
					dataType: "json",
					data: {
						username: username,
						goodsname: $(ele).parents(".goodMsg").find("h5").text()
					},
					type: "post",
					success: function (res) {
						console.log(res);
					}
				})
				var index = $(ele).parents(".goodMsg").index();
				console.log($(ele));
				console.log(index);
				$(ele).parents(".goodMsg").remove();
				$("dl").eq(index).remove();
				priceSum();
			}
		})
		
	})
	
	
})